package com.ms3ds.deviceData.service.impl;

import java.util.*;

import com.ms3ds.common.core.domain.R;
import com.ms3ds.common.core.utils.DateUtils;
import com.ms3ds.mineInfo.api.RemoteAreaService;
import com.ms3ds.mineInfo.api.domain.Area;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ms3ds.deviceData.mapper.MiningRecordMapper;
import com.ms3ds.deviceData.domain.MiningRecord;
import com.ms3ds.deviceData.service.IMiningRecordService;

/**
 * 矿石开采记录Service业务层处理
 * 
 * @author zlw
 * @date 2025-09-21
 */
@Service
public class MiningRecordServiceImpl implements IMiningRecordService 
{
    @Autowired
    private MiningRecordMapper miningRecordMapper;

    @Autowired
    private RemoteAreaService remoteAreaService;


    /**
     * 查询矿石开采记录
     * 
     * @param id 矿石开采记录主键
     * @return 矿石开采记录
     */
    @Override
    public MiningRecord selectMiningRecordById(Long id)
    {
        return miningRecordMapper.selectMiningRecordById(id);
    }

    @Override
    public Float getAreaTotalMiningRecord(Long areaId) {
        return miningRecordMapper.selectAreaTotalMiningRecord(areaId);
    }

    @Override
    public Float getTeamTotalMiningRecord(Long teamId) {
        return miningRecordMapper.selectTeamTotalMiningRecord(teamId);
    }


    /**
     * 查询矿石开采记录列表
     * 
     * @param miningRecord 矿石开采记录
     * @return 矿石开采记录
     */
    @Override
    public List<MiningRecord> selectMiningRecordList(MiningRecord miningRecord)
    {
        return miningRecordMapper.selectMiningRecordList(miningRecord);
    }

    @Override
    public Float selectAvgPingWeiByOreId(MiningRecord miningRecord) {
        return miningRecordMapper.selectAvgPingWeiByOreId(miningRecord);
    }

    @Override
    public Float selectMiningRecordSum(MiningRecord miningRecord) {
        return miningRecordMapper.selectMiningRecordSum(miningRecord);
    }

    /**
     * 新增矿石开采记录
     * 
     * @param miningRecord 矿石开采记录
     * @return 结果
     */
    @Override
    public int insertMiningRecord(MiningRecord miningRecord)
    {
        return miningRecordMapper.insertMiningRecord(miningRecord);
    }

    /**
     * 修改矿石开采记录
     * 
     * @param miningRecord 矿石开采记录
     * @return 结果
     */
    @Override
    public int updateMiningRecord(MiningRecord miningRecord)
    {
        return miningRecordMapper.updateMiningRecord(miningRecord);
    }

    /**
     * 批量删除矿石开采记录
     * 
     * @param ids 需要删除的矿石开采记录主键
     * @return 结果
     */
    @Override
    public int deleteMiningRecordByIds(Long[] ids)
    {
        return miningRecordMapper.deleteMiningRecordByIds(ids);
    }

    /**
     * 删除矿石开采记录信息
     * 
     * @param id 矿石开采记录主键
     * @return 结果
     */
    @Override
    public int deleteMiningRecordById(Long id)
    {
        return miningRecordMapper.deleteMiningRecordById(id);
    }

    @Override
    public List<Float> getMiningRecordSevenDaySum(Long mineId) {
        List<Float> result = new ArrayList();
        int searchCount = 7;
        Date[] yesterdayRange = DateUtils.getYesterdayRange();
        for(int i =0;i<searchCount;i++){
            MiningRecord searchMiningRecord = new MiningRecord();
            //设置查询区域
            searchMiningRecord.setMineId(mineId);
            //设置查询时间
            Map<String,Object> params = new HashMap<>();
            Date startTime = org.apache.commons.lang3.time.DateUtils.addDays(yesterdayRange[0], 1-i);
            Date endTime = org.apache.commons.lang3.time.DateUtils.addDays(yesterdayRange[1], 1-i);
            params.put("startTime",DateUtils.paraseString(startTime));
            params.put("endTime",DateUtils.paraseString(endTime));
            searchMiningRecord.setParams(params);
            //查询矿山的开采总量
            Float sum = miningRecordMapper.selectMiningRecordSumByMineId(searchMiningRecord);
            result.add(sum);
        }
        //进行倒叙，按照-6 -5 -4 -3 -2 -1 0(今天)的顺序排列
        Collections.reverse(result);
        return result;
    }

    @Override
    public Float getMiningRecordSumByMineId(MiningRecord miningRecord) {
        return miningRecordMapper.selectMiningRecordSumByMineId(miningRecord);
    }
}
